﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Resources;
using System.ComponentModel;
using VIBlend.WPF.Controls;
using System.Reflection;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class RowDetailsDemo : Window, IWindow
    {
        DataTemplate rowDetailsTemplate;
        public void SetTheme()
        {
            dataGrid.FontFamily = new FontFamily("Tahoma");
          
        }
  
        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }

        public RowDetailsDemo()
        {
            InitializeComponent();

            PrepareGridData();

            this.rowDetailsTemplate = Resources["rowDetailsTemplate"] as DataTemplate;

            dataGrid.RowsHierarchy.Visible = false;

            dataGrid.ItemsSource = listOfBooks;

            this.dataGrid.ColumnsHierarchy.AllowResize = true;
        
            dataGrid.ColumnsHierarchy.Items[0].Width = 525;
            dataGrid.ColumnsHierarchy.Items[1].Width = 110;
            dataGrid.ColumnsHierarchy.Items[2].Width = 48;

            foreach (HierarchyItem row in dataGrid.RowsHierarchy.Items)
            {
                row.RowDetailsDataTemplate = this.rowDetailsTemplate;
                row.RowDetailsHeight = 120;
            }

            dataGrid.SelectionBorderEnabled = false;
            dataGrid.SelectionMode = VIBlend.WPF.Controls.DataGrid.SELECTION_MODE.FULL_ROW_SELECT;

            dataGrid.LoadRowDetails += new EventHandler<RowDetailsEventArgs>(dataGrid_LoadRowDetails);

            dataGrid.CellMouseClick += new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid_CellMouseClick);

            dataGrid.RowsHierarchy.Items[2].RowDetailsVisiblity = Visibility.Visible;

            dataGrid.ColumnsHierarchy.AllowResize = false;

            dataGrid.Refresh();
        }

        void dataGrid_CellMouseClick(object sender, CellMouseButtonEventArgs args)
        {
            int row = args.Cell.RowItem.BoundFieldIndex;
            dataGrid.RowsHierarchy.Items[row].RowDetailsVisiblity = (dataGrid.RowsHierarchy.Items[row].RowDetailsVisiblity == Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible;
        }

        void dataGrid_LoadRowDetails(object sender, RowDetailsEventArgs e)
        {
            FrameworkElement element = e.RowDetailsElement;
            element.DataContext = listOfBooks[e.RowItem.BoundFieldIndex];
        }

        #region Sample Data

        public class Book
        {
            public Book(
                string Title,
                string Details,
                string Author,
                string ISBN,
                string PublishDate,
                double Price,
                string Image)
            {
                this.Title = Title;
                this.Details = Details;
                this.Author = Author;
                this.ISBN = ISBN;
                this.PublishDate = DateTime.Parse(PublishDate);
                this.Price = Price;
                this.Image = Image;
            }

            public string Title { get; set; }
            public string Details { get; set; }
            public string Author { get; set; }
            public string ISBN { get; set; }
            public DateTime PublishDate { get; set; }
            public double Price { get; set; }
            public string Image { get; set; }
        }

        public List<Book> listOfBooks = new List<Book>();

        private void PrepareGridData()
        {
            try
            {
                StreamResourceInfo sri = Application.GetResourceStream(new Uri("ExamplesCSharp;component/CSharp/DataGridViewDemos/books.tab", UriKind.Relative));
                System.IO.StreamReader sr = new System.IO.StreamReader(sri.Stream);
                string line = null;
                sr.ReadLine();
                while ((line = sr.ReadLine()) != null)
                {
                    string[] tabs = line.Split('\t');
                    listOfBooks.Add(new Book(tabs[0], tabs[1], tabs[2], tabs[3], tabs[4], double.Parse(tabs[5]), tabs[6]));
                }
                sr.Close();
            }
            catch (System.Exception)
            {
            }
        }
        #endregion

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            this.listOfBooks.Clear();
            this.listOfBooks = null;
            this.dataGrid.LoadRowDetails -= new EventHandler<RowDetailsEventArgs>(dataGrid_LoadRowDetails);
            this.dataGrid.CellMouseClick -= new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid_CellMouseClick);

            this.dataGrid.Dispose();
            this.dataGrid = null;
        }
    }
}
